import { createRouter, createWebHistory } from 'vue-router'
import Base from '../components/layout/Base.vue'
import  userInfoStore from '../store/user.js'

// 不需要权限的页面
const constantRoutes = [
  {
    path: '/',
    name: 'base',
    component: Base,
    redirect: '/index',
    children: [
      //主页
      {
        path: '/index',
        name: 'index',
        component: import('../views/index/index.vue'),
      },
      // 关于我
      {
        path: '/about',
        name: 'about',
        component: import('../views/about/about.vue'),
      },
      // 留言
      {
        path: '/message',
        name: 'message',
        component: import('../views/message/message.vue'),
      },
      // 发表
      {
        path: '/publish',
        name: 'publish',
        component: import('../views/publish/publish.vue'),
      }
    ],    
  },
  //登录
  {
    path: '/login',
    name: 'login',
    component: import('../views/login/login.vue'),
  }
  // // 404
  // {
  //   path: '/:catchAll(.*)',
  //   name: '404',
  //   component: () => import('../components/waring/404.vue')
  // },
  // // 500
  // {
  //   path: '/500',
  //   name: '500',
  //   component: () => import('../components/waring/500.vue')
  // },
];

const router = createRouter({
  history: createWebHistory('/'),
  routes: constantRoutes
});

// router.beforeEach((to, from, next) => {
//   // const user = userInfoStore()
//   // 切换router时，取消pending中的请求
//   if (window.__axiosPromiseArr) {
//     window.__axiosPromiseArr.forEach((ele, ind) => {
//       ele.cancel();
//       delete window.__axiosPromiseArr[ind];
//     });
//   }
  // // token过期时间
  // if (localStorage.getItem('expires') && (new Date().getTime() - localStorage.getItem('expires')) / 1000 > 1) {
  //   $uiMsg.error('登录失效，请重新登录', () => {
  //     localStorage.removeItem('userInfon');
  //     localStorage.removeItem('token');
  //     localStorage.removeItem('expires');
  //     location.href = '/login';
  //   });
  //   return;
  // }
  // // 登录判断
  // if (user.token) {
  //   if (to.path === '/login') {
  //     next({ path: '/' });
  //   } else {
  //     // 权限判断
  //     next();
  //   }
  // } else {
  //   if (to.path === '/login') {
  //     next();
  //   } else {
  //     next({ name: 'login' });
  //   }
  // }
// });

// 跳转完成后，将滚动条位置重置
router.afterEach(to => {
  window.scrollTo(0, 0);
});

export default router;